<?php

namespace App\Model\Guild;

use App\Model\GuildManage;
use App\Model\User;
use App\Model\UserProfile;
use Encore\Admin\Facades\Admin;

/**
 * 代理女用户管理
 */
class GuildHost extends GuildBase
{
    protected $table = 'guild_host_manage';
    protected $fillable = [
        "status", "unbind_at"
    ];

    public function hostInfo()
    {
        return $this->belongsTo(UserProfile::class, 'host_user_id', 'user_id');
    }

    public function hostUserInfo()
    {
        return $this->belongsTo(User::class, 'host_user_id', 'id');
    }

    // public function personalInfo()
    // {
    //     return $this->belongsTo(UserProfilePersonal::class, 'host_user_id', 'user_id');
    // }

    public function guildInfo()
    {
        return $this->belongsTo(Guild::class, 'guild_manage_id', 'id');
    }

    /**
     * 获取代理女用户数
     */
    static function getHostNum($guildId = 0)
    {
        return self::where(['guild_manage_id' => $guildId])->count();
    }

    /**
     * 女用户是否绑定代理
     */
    static function checkBind($hostId = 0)
    {
        $where = ['host_user_id' => $hostId, 'status' => 1];
        return self::where($where)->orderBy('id', 'desc')->first();
    }

    /**
     * 根据女用户ID获取代理ID
     */
    static function getGuildId($hostId)
    {
        $item = self::checkBind($hostId);
        return $item ? $item->guild_manage_id : 0;
    }

    /**
     * 查询所有状态正常的渠道
     * @return array
     */
    static function getAllHosts()
    {
        $items =  self::where(['status' => 1])->get();
        $guildInfo = Guild::getInfo();
        $ret = [];
        foreach ($items as &$item) {
            $ret[$item->host_user_id] = $guildInfo[$item['guild_manage_id']] ?? '';
        }
        return $ret;
    }

    /**
     * 查询所有渠道包括停用的渠道
     * @return array
     */
    static function getAllHostsStatusAll()
    {
        $items =  self::get();
        $guildInfo = Guild::getInfo();
        $ret = [];
        foreach ($items as &$item) {
            $ret[$item->host_user_id] = $guildInfo[$item['guild_manage_id']] ?? '';
        }
        return $ret;
    }

    /**
     * 获取女用户
     */
    static function getHostIds($guildId = 0)
    {
        $where = [
            ['status', 1],
        ];
        if ($guildId && is_array($guildId)) {
            return self::where($where)->whereIn('guild_manage_id', $guildId)->pluck('host_user_id')->toArray();
        }
        $guildId && $where[] = ['guild_manage_id', $guildId];
        return self::where($where)->pluck('host_user_id')->toArray();
    }

    static function getHostUidByAdminId($guidIds = [])
    {
        $where = [
            ['status', 1],
        ];

        return self::where($where)->whereIn('guild_manage_id',$guidIds)->pluck('host_user_id')->toArray();
    }
}
